//
//  MainDiscernTbv.m
//  ZITHER
//
//  Created by weixin on 2018/11/27.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "MainDiscernTbv.h"
#import "MainDiscernTbvShootCell.h"
#import "MainDiscernTbvCellPic.h"
#import "XFCameraController.h"
#import "MainDiscernVC.h"
#import "MainDiscernSelectWatchVC.h"

@implementation MainDiscernTbv


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
     
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}




#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        return [self getMainDiscernTbvCellPic:indexPath];
    }else{
        return [self getMainDiscernTbvShootCell:indexPath];
    }
}

-(UITableViewCell *)getMainDiscernTbvShootCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"MainDiscernTbvShootCell";
    MainDiscernTbvShootCell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDiscernTbvShootCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
    
}
-(UITableViewCell *)getMainDiscernTbvCellPic:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"MainDiscernTbvCellPic";
    MainDiscernTbvCellPic *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainDiscernTbvCellPic alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.stare = defaul;

    //    cell.model = self.model;
    //    cell.indexPath = index;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==1) {
        XFCameraController *cameraController = [XFCameraController defaultCameraController];
        
        __weak XFCameraController *weakCameraController = cameraController;
        
        cameraController.takePhotosCompletionBlock = ^(UIImage *image, NSError *error) {
            NSLog(@"takePhotosCompletionBlock");
            
            [weakCameraController dismissViewControllerAnimated:YES completion:nil];
        };
        
        cameraController.shootCompletionBlock = ^(NSURL *videoUrl, CGFloat videoTimeLength, UIImage *thumbnailImage, NSError *error) {
            NSLog(@"shootCompletionBlock");
            [weakCameraController dismissViewControllerAnimated:YES completion:^{
//                MainDiscernSelectWatchVC *vc = [[MainDiscernSelectWatchVC alloc]init];
//                [self.getNav pushViewController:vc animated:YES];
            }];
            [QMUITips showLoadingInView:self];
            
            [self upLoad:videoUrl];
        };
        
        MainDiscernVC *vc = (MainDiscernVC*)[self getViewController:[MainDiscernVC class]];
        [vc presentViewController:cameraController animated:YES completion:nil];
    }
}


-(void)upLoad :(NSURL*)url{
    
    [[serviceDefualt loadupVideoreponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            TXPublishParam *videoPublishParams = [[TXPublishParam alloc] init];
            videoPublishParams.signature  = x.responseRawData[@"data"];
            videoPublishParams.coverPath = nil;
            NSString * urlSTR = [url absoluteString];
            urlSTR = [urlSTR componentsSeparatedByString:@"/Documents"][1];
            urlSTR =  [NSString stringWithFormat:@"%@/Documents%@",NSHomeDirectory(),urlSTR];
            videoPublishParams.videoPath  = urlSTR;
            [self.videoPublish publishVideo:videoPublishParams];
        }else{
             [Tools showTips:x.errorInfo];
        }
    }];
    
}

-(TXUGCPublish *)videoPublish{
    if (!_videoPublish) {
        _videoPublish = [[TXUGCPublish alloc] initWithUserID:@"independence_ios"];
        _videoPublish.delegate = self;
        //    [_videoPublish canclePublish];    取消上传
    }
    return _videoPublish;
}

#pragma mark - TXVideoPublishListener


-(void) onPublishComplete:(TXPublishResult*)result
{
    if (result.retCode == 0) {
//        [Tools showTips:@"上传成功"];
        self.videoID = result.videoId;
        
        [[serviceDefualt loadVideosaveVideoId:self.videoID id:@"" goodId:@"" seriesId:@"" brandId:@"" goodName:@"" seriesName:@"" brandName:@"" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
            [QMUITips hideAllTipsInView:self];
            if (x.success) {
                self.id = x.responseRawData[@"data"][@"id"];
                //                [Tools showTips:@"上传腕表视频信息成功"];
            }else{
                //                [Tools showTips:@"上传腕表视频信息失败"];
            }
        }];
        
//        [serviceDefualt loadVideosaveVideoId:result.videoId id:@"" goodId:@"" seriesId:@"" brandId:@"" goodName:@"" seriesName:@"" brandName:@"" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
//
//        }];
        
    }else{
        [QMUITips hideAllTipsInView:self];

        [Tools showTips:@"上传失败"];
        NSString *string = [NSString stringWithFormat:@"上传完成，错误码[%d]，信息[%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg];
        [Tools showTips:string];
        NSLog(@"onPublishComplete [%d/%@]", result.retCode, result.retCode == 0? result.videoURL: result.descMsg);
    }
  
}

#pragma mark - TXVideoPublishListener
-(void) onPublishProgress:(NSInteger)uploadBytes totalBytes: (NSInteger)totalBytes
{
    NSLog(@"onPublishProgress [%lld/%lld]", uploadBytes, totalBytes);
}

@end
